package com.gms.service.usermanager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gms.entity.user.Notice;
import com.gms.entity.user.User;
import com.gms.mapper.NoticeMapper;
import com.gms.service.usermanager.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

/**
 * 实现类
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override //添加公告
    public void add_notice(Notice notice){

        noticeMapper.insert(notice);
    }

    @Override //删除公告
    public void delete_notice(Integer noticeID) {
        noticeMapper.deleteById(noticeID);
    }

    @Override //修改公告
    public void modify_notice(Notice notice){
        noticeMapper.updateById(notice);
    }

    @Override  //向页面返回公告列
    public Notice list_notice() {
        Notice notice = noticeMapper.selectById(2);
        return notice;
    }

    @Override
    public void update_notice(Notice notice) {
        noticeMapper.updateById(notice);
    }

    @Override
    public Notice select_one(Integer noticeId){
        return noticeMapper.selectById(noticeId);
    };

    @Override
    public Page<Notice> selectPage(int pageNumber, String keyword) {
        //第二个参数size表示每页显示5条
        Page<Notice> page=new Page(pageNumber,5);
        QueryWrapper<Notice> Wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            Wrapper.and(
                    wrapper ->
                            wrapper.like("title", keyword).or().like("content", keyword)
            );

        }
        return noticeMapper.selectPage(page, Wrapper);
    }
}
